<!DOCTYPE html>

<!-- script标签会把请求过来的响应数据当做JS脚本自动执行 -->
<script src="/login/status"></script>
<script>
  console.log(isLogin, 'isLogin');
  if (!isLogin) {
    location.href = '/admin/login.html'
  }
</script>

<html lang="zh-CN">

<head>
  <meta charset="utf-8">
  <title>Settings &laquo; Admin</title>
  <link rel="stylesheet" href="/assets/vendors/bootstrap/css/bootstrap.css">
  <link rel="stylesheet" href="/assets/vendors/font-awesome/css/font-awesome.css">
  <link rel="stylesheet" href="/assets/vendors/nprogress/nprogress.css">
  <link rel="stylesheet" href="/assets/css/admin.css">
  <script src="/assets/vendors/nprogress/nprogress.js"></script>
</head>

<body>
  <script>NProgress.start()</script>

  <div class="main">
    <nav class="navbar">
      <button class="btn btn-default navbar-btn fa fa-bars"></button>
      <ul class="nav navbar-nav navbar-right">
        <li><a href="profile.html"><i class="fa fa-user"></i>个人中心</a></li>
        <li><a href="login.html"><i class="fa fa-sign-out"></i>退出</a></li>
      </ul>
    </nav>
    <div class="container-fluid">
      <div class="page-title">
        <h1>网站设置</h1>
      </div>
      <!-- 有错误信息时展示 -->
      <!-- <div class="alert alert-danger">
        <strong>错误！</strong>发生XXX错误
      </div> -->
      <form class="form-horizontal" id="form">
        <div class="form-group">
          <label for="site_logo" class="col-sm-2 control-label">网站图标</label>
          <div class="col-sm-6">
            <input id="hiddenLogo" name="logo" type="hidden">
            <label class="form-image">
              <input id="logo" type="file">
              <img src="/assets/img/logo.png" id="previewImage">
              <i class="mask fa fa-upload"></i>
            </label>
          </div>
        </div>
        <div class="form-group">
          <label for="title" class="col-sm-2 control-label">站点名称</label>
          <div class="col-sm-6">
            <input id="title" name="title" class="form-control" type="type" placeholder="站点名称">
          </div>
        </div>
        <div class="form-group">
          <label for="description" class="col-sm-2 control-label">站点描述</label>
          <div class="col-sm-6">
            <textarea id="description" name="description" class="form-control" placeholder="站点描述" cols="30"
              rows="6"></textarea>
          </div>
        </div>
        <div class="form-group">
          <label for="keywords" class="col-sm-2 control-label">站点关键词</label>
          <div class="col-sm-6">
            <input id="keywords" name="keywords" class="form-control" type="type" placeholder="站点关键词">
          </div>
        </div>
        <div class="form-group">
          <label class="col-sm-2 control-label">评论</label>
          <div class="col-sm-6">
            <div class="checkbox">
              <label><input id="comment" name="comment" type="checkbox" checked value="true">开启评论功能</label>
            </div>
            <div class="checkbox">
              <label><input id="review" name="review" type="checkbox" checked value="true">评论必须经人工批准</label>
            </div>
          </div>
        </div>
        <div class="form-group">
          <div class="col-sm-offset-2 col-sm-6">
            <button type="submit" class="btn btn-primary">保存设置</button>
          </div>
        </div>
      </form>
    </div>
  </div>

  <div class="aside">
    <div class="profile">
      <img class="avatar" src="/uploads/avatar.jpg">
      <h3 class="name">Coder</h3>
    </div>
    <ul class="nav">
      <li>
        <a href="index.html"><i class="fa fa-dashboard"></i>仪表盘</a>
      </li>
      <li>
        <a href="#menu-posts" class="collapsed" data-toggle="collapse">
          <i class="fa fa-thumb-tack"></i>文章<i class="fa fa-angle-right"></i>
        </a>
        <ul id="menu-posts" class="collapse">
          <li><a href="posts.html">所有文章</a></li>
          <li><a href="post-add.html">写文章</a></li>
          <li><a href="categories.html">分类目录</a></li>
        </ul>
      </li>
      <li>
        <a href="comments.html"><i class="fa fa-comments"></i>评论</a>
      </li>
      <li>
        <a href="users.html"><i class="fa fa-users"></i>用户</a>
      </li>
      <li class="active">
        <a href="#menu-settings" data-toggle="collapse">
          <i class="fa fa-cogs"></i>设置<i class="fa fa-angle-right"></i>
        </a>
        <ul id="menu-settings" class="collapse in">
          <li><a href="slides.html">图片轮播</a></li>
          <li class="active"><a href="settings.html">网站设置</a></li>
        </ul>
      </li>
    </ul>
  </div>

  <script src="/assets/vendors/jquery/jquery.js"></script>
  <script src="/assets/vendors/bootstrap/js/bootstrap.js"></script>
  <script>NProgress.done()</script>

  <script>
    $(() => {

      // 获取现有的设置
      $.ajax({
        type: 'GET',
        url: '/settings',
        success(res) {
          $('#previewImage').attr('src', res.logo).show()
          $('#hiddenLogo').val(res.logo)

          $('#title').val(res.title)
          $('#description').val(res.description)
          $('#keywords').val(res.keywords)

          $('#comment').prop('checked', res.comment)
          $('#review').prop('checked', res.review)
        }
      })

      $('#logo').change(({ target }) => {
        const fd = new FormData()
        fd.append('image', target.files[0])
        $.ajax({
          type: 'POST',
          url: '/upload',
          data: fd,
          processData: false,
          contentType: false,
          success(res) {
            const url = res[0].image
            $('#previewImage').attr('src', url).show()
            $('#hiddenLogo').val(url)
          }
        })
      })

      $('#form').submit(({ target }) => {

        // // 通过这种方式获取到的表单数据，如果其中有复选框，复选框又没选中，那么是不会在表单数据中的
        // const data = $(target).serialize()

        // console.log(data);

        // var fd = new FormData(target)

        // fd.set('comment', $('#comment').prop('checked'))
        // fd.set('review', $('#review').prop('checked'))

        // for (const [k, v] of fd.entries()) {
        //   console.log(k, v);
        // }

        const data = {
          title: $('#title').val(),
          description: $('#description').val(),
          keywords: $('#keywords').val(),
          comment: $('#comment').prop('checked'),
          review: $('#review').prop('checked'),
          logo: $('#hiddenLogo').val()
        }

        // console.log(data);

        $.ajax({
          type: 'POST',
          url: '/settings',
          data,
          success(res) {
            console.log(res);
          }
        })

        return false
      })

    })
  </script>
</body>

</html>